﻿using SvGame.Objects;

namespace SvGame.Sv;

public class GameCmds : IExit
{
    [CoreCmd("save", "存储")]
    public void Save(CmdInfo cmd)
    {
        loger.Info("存档");
        foreach (var item in DictPlayer.Values)
        {
            DbRedis.ZSave(item);
        }
    }
    [CoreCmd("exit", "关闭服务器")]
    public void 关闭服务器(CmdInfo cmd)
    {
        loger.Info("存档");
        foreach (var item in DictPlayer.Values)
        {
            item.PlayerLogout(false);
            DbRedis.ZSave(item);
        }
        Core.ManualStop.Set();
    }
    public void Destroy()
    {
        关闭服务器(null);
    }
    [CoreCmd("map", "显示地图")]
    public void Map(CmdInfo cmd)
    {
        var mapName = string.Empty;
        if (cmd.Args.Length >= 2)
        {
            mapName = cmd.Args[1];
        }
        if (mapName == "all")
        {
            loger.Info("世界地图：");
            var sb = new StringBuilder();
            //foreach (var item in ConfCity.Values)
            //{
            //    loger.Info($"name:{item.Name} CityX:{item.CityX} CityY:{item.CityY}");
            //}
            foreach (var item in ConfCity.Values)
            {
                sb.AppendLine($"id:{item.Id} name:{item.Name} CityX:{item.CityX} CityY:{item.CityY}");
            }
            var listx = ConfCity.Values.Select(x => x.CityX).ToList()
                .Distinct().ToList().OrderBy(it => it).ToList();
            //loger.Info("listx");
            //foreach (var item in listx)
            //{
            //    loger.Info(item);
            //}
            var listy = ConfCity.Values.Select(x => x.CityY).ToList().Distinct()
                .Distinct().ToList().OrderBy(it => it).ToList();
            //loger.Info("listy");
            //foreach (var item in listy)
            //{
            //    loger.Info(item);
            //}
            for (int j = listy.Count - 1; j >= 0; j--)
            {
                var sbLine = new StringBuilder();
                for (int i = 0; i < listx.Count; i++)
                {
                    var uid = listx[i] * 100 + listy[j];
                    string name = "";
                    if (ConfCity.TryGetValue(uid, out var conf))
                    {
                        name = conf.Name;
                    }
                    if (name.Length < 5)
                    {
                        var nameAdd = string.Empty;
                        for (int ii = 0; ii < 5 - name.Length; ii++)
                        {
                            nameAdd += "  ";
                        }
                        name += nameAdd;
                    }
                    sbLine.Append(name);
                    //sbLine.Append(string.Format("{0,-10}", name));
                }
                //loger.Info(sbLine.ToString());
                sb.AppendLine(sbLine.ToString());
            }
            Console.WriteLine(sb.ToString());
            return;
        }
        ConfigCity confCity = null;
        foreach (var city in ConfCity.Values)
        {
            if (city.Id.ToString() == mapName || city.Name == mapName)
            {
                confCity = city;
                break;
            }
        }
        if (confCity != null)
        {
            loger.Info($"{confCity.Name}地图 [{confCity.Id}] ：");
            var listRoom = ConfRoom.Values.Where(it => it.CityUid == confCity.Id).Select(it=>it).ToList();
            loger.Info(listRoom.Count);

            var sb = new StringBuilder();
            var listx = listRoom.Select(x => x.RoomX).ToList()
                .Distinct().ToList().OrderBy(it => it).ToList();
            var listy = listRoom.Select(x => x.RoomY).ToList().Distinct()
                .Distinct().ToList().OrderBy(it => it).ToList();
            for (int j = listy.Count - 1; j >= 0; j--)
            {
                var sbLine = new StringBuilder();
                for (int i = 0; i < listx.Count; i++)
                {
                    var uid = confCity.Id * 1_0000 + listx[i] * 100 + listy[j];
                    string name = "";
                    if (ConfRoom.TryGetValue(uid, out var conf))
                    {
                        name = conf.Name.ColorRemove();
                    }
                    if (name.Length < 5)
                    {
                        var nameAdd = string.Empty;
                        for (int ii = 0; ii < 5 - name.Length; ii++)
                        {
                            nameAdd += "  ";
                        }
                        name += nameAdd;
                    }
                    sbLine.Append(name);
                }
                sb.AppendLine(sbLine.ToString());
            }
            Console.WriteLine(sb.ToString());

            return;
        }

        loger.Info("显示地图");
        loger.Info("map all|城市名");
    }
    [CoreCmd("test", "调试")]
    public void Test(CmdInfo cmd)
    {
        {
            var list = new List<string>();
            for (int i = 0; i < 20; i++)
            {
                list.Add(NameMgr.GetName());
            }
            Console.WriteLine(Jsh.Jstr(list));
        }
    }
}
